// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <deque>
#include <pollux/serializers/presto_serializer.h>

#include <tests/exec/JoinSpillInputBenchmarkBase.h>

using namespace kumo::pollux;
using namespace kumo::pollux::common;
using namespace kumo::pollux::memory;
using namespace kumo::pollux::exec;

namespace kumo::pollux::exec::test {
namespace {
const int numSampleVectors = 100;
} // namespace

void JoinSpillInputBenchmarkBase::setUp() {
  SpillerBenchmarkBase::setUp();
  common::SpillConfig spillConfig;
  spillConfig.getSpillDirPathCb = [&]() -> std::string_view {
    return spillDir_;
  };
  spillConfig.updateAndCheckSpillLimitCb = [&](uint64_t) {};
  spillConfig.fileNamePrefix = FLAGS_spiller_benchmark_name;
  spillConfig.writeBufferSize = FLAGS_spiller_benchmark_write_buffer_size;
  spillConfig.executor = executor_.get();
  spillConfig.compressionKind =
      stringToCompressionKind(FLAGS_spiller_benchmark_compression_kind);
  spillConfig.maxFileSize = FLAGS_spiller_benchmark_max_spill_file_size;
  spillConfig.maxSpillRunRows = 0;
  spillConfig.fileCreateConfig = {};

  spiller_ = std::make_unique<NoRowContainerSpiller>(
      rowType_, HashBitRange{29, 29}, &spillConfig, &spillStats_);
  dynamic_cast<NoRowContainerSpiller*>(spiller_.get())
      ->setPartitionsSpilled({0});
}

void JoinSpillInputBenchmarkBase::run() {
  MicrosecondTimer timer(&executionTimeUs_);
  auto noRowContainerSpiller =
      dynamic_cast<NoRowContainerSpiller*>(spiller_.get());
  POLLUX_CHECK_NOT_NULL(noRowContainerSpiller);
  for (auto i = 0; i < numInputVectors_; ++i) {
    noRowContainerSpiller->spill(0, rowVectors_[i % numSampleVectors]);
  }
}

} // namespace kumo::pollux::exec::test
